<?
include_once("includes/header.php");

function GetSelected($query)
{
    $array;
    $result = mysql_query($query);
    if ($result)
    {
        for ($i = 0; $i < mysql_num_rows($result); $i++)
        {
            $ret[mysql_result($result, $i)] = true;
        }
        return isset($ret) ? $ret : false;
    }
    else
        return false;
}

$imagenes = "";
?>
</head>
<body>
    <div id="no-print">
        <?
        $titulo = 'Diagn&oacute;stico';
        include('menu.php');
        if (isset($_SESSION["protocolo"]))
        {
            if (is_numeric($_SESSION["protocolo"]))
            {
                $protocolo = $_SESSION["protocolo"];
                $pap = GetSQLFirstValue("select id_pap from pap where numero_protocolo = " . $protocolo);
                if ($pap)
                {
                    $diagnostico = GetSelected("select codigo_diagnostico_pap from pap_diagnostico_pap where id_pap = " . $pap);
                }
                $query = "select numero_imagen, numero_protocolo, url, descripcion from protocolo_imagen where numero_protocolo =" . $protocolo;
                $imagenes = GetSQL($query);
            }
        }
        $query = "select codigo from template_pap_diagnostico_pap group by codigo order by codigo";
        ?>
        <link type="text/css" href="styles/printImage.css" rel="stylesheet" media="print" />
        <script type="text/javascript" src="js/jquery.fancyzoom.min.js"></script>
        <script type="text/javascript">
            $(function(){
        
                $(".print").click(function(event){                   
                    event.preventDefault();
                    $("#printImage").html("");
                    $("input[name='imprimirImagen']:checked").each(function(){                
                        $("#printImage").append("<img src='" + $(this).val() + "' alt='imagen'/><br/>");
                    });
                    setTimeout(function(){print()}, 500);
                });
                $(".zoom").fancyzoom({imgDir: "./imagenes/"});
<? if (isset($protocolo))
{ ?>
                    $("#fieldsetDiagnostico").append("<div><a href='#' id='linkImagenes'>Im&aacute;genes</a></div>");
                    $("#fieldsetDiagnostico").append("<div class='save'><a href='#' id='saveButton'><img alt='guardar' title='Guardar' src='imagenes/guardar.png'/></a></div>");
<? } ?>
                $("#botonPlantilla").click(function(){
                    $("#formulario").load("cargaTemplatePapDiagnostico.php?codigo=" + $("#plantilla").val());
                });
                $("#imagenes").dialog(
                {
                    autoOpen: false,
                    title: "Im&aacute;genes",
                    width: 800,
                    open: function() { $("#linkImagenes").hide();},
                    close: function() { $("#linkImagenes").show();}
                });
                $("#linkImagenes").click(function(event){
                    $("#imagenes").dialog("open");
                    event.preventDefault();
                });
                $("#saveButton").live("click", function(event){
                    $("#formDiagnostico").submit();
                    event.preventDefault();
                });
<?
if (isset($_GET["imagen"]))
{
    ?>
                        $("#imagenes").dialog("open");  
<? } ?>
            });
        </script>
        <div class="pageContainer">
            <form id="formDiagnostico" action="guardaPapDiagnostico.php" method="post">
                <? include 'includes/inputPlantilla.php' ?>
                <div id="formulario">
                    <? include "papDiagnostico.php"; ?>
                </div>
            </form>
        </div>
        <div id="imagenes">       
            <?
            if ($imagenes)
            {
                $numImagen = 1;
                ?>
                <table id="tablaImagenes"> 
                    <?
                    foreach ($imagenes as $imagen)
                    {
                        ?>
                        <tr >

                            <td>
                                <?= $numImagen . " " . $imagen["descripcion"] ?> 
                            </td>
                            <td>
                                <a class="zoom" href="<?= UPLOAD_PATH . $imagen["url"] ?>">ver</a>
                            </td>
                            <td>
                                <input id="img_<?= $numImagen ?>" type="checkbox" name="imprimirImagen" value="<?= UPLOAD_PATH . $imagen["url"] ?>"/> 
                                <label for="img_<?= $numImagen ?>">Imprimir</label>
                            </td>
                        </tr>
                    <? } ?>
                </table>
            <? }
            ?>        
            <a class= "print" href="#">Imprimir</a>
            <form enctype="multipart/form-data" method="post" action="guardarImagen.php">
                <input type="file" name="img" id="img" />
                <div><label class="blackLabel">Descripci&oacute;n </label> <input type="text" name="descripcion" id="descripcion" /></div>
                <input type="hidden" name="returnUrl" id="returnUrl" value="<?= $_SERVER["PHP_SELF"] ?>"/>
                <button type="submit">Guardar</button>
            </form>
        </div>
    </div>
    <div id="printImage"></div>
</body>
</html>